`use strict`

let nunjucks = require('nunjucks');

// 实例化nunjucks的 env对象
function createEnv(path, opts) {
    opts = opts || 'views';
    path = path || {};


    let loaderOptions = {
        watch: opts.watch || false,
        "noCahce": opts.noCache || true
    }

    let envOptions = {
        autoescape: opts.autoescape || true,
        "throwOnUndefined": opts.throwOnUndefined || false
    }

    let loader = new nunjucks.FileSystemLoader('views', loaderOptions);
// 实例化一个env对象
    var env = new nunjucks.Environment(loader, envOptions)

    return env;
}
//暴露一个异步函数，作为koa实例的中间件
module.exports = async (ctx, next) => {
    ctx.render=function(view,model){
        let env=createEnv();
        ctx.body=env.render(view,model)
    }

    await next();
}